using UnityEngine;
using System.Collections;

public class AntiCampSwarm : MonoBehaviour {
	public GameObject 			camper;						// The player the swarm will be destined to follow
	public float 				speed = 0.001f;				// The speed the swarm will move in order to prevent crawling players
	public float 				timeUntilDamage = 120f;		// The time the player must be within reach of the swarm to be active.
	public float 				damageCooldown = 5f;		// The time between attacks
	public int 					damage = 2;					// The damage done each damageCooldown
	public float				dropDistance = 30f;			// The distance the player must be away from the swarm for the swarm to die
	public float				timeUntilKillSwarm = 5f; 	// The time it takes the swarm to die once the player is out of reach
	
	private float 				cooldownTimer;
	private float 				timeAlive;
	private float				timer2;
	
	public bool					debug;
	
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		if( camper != null){
			timeAlive += Time.deltaTime;
			gameObject.transform.position = Vector3.Slerp(gameObject.transform.position, camper.transform.position, speed);
			if(timeAlive > timeUntilDamage){
				if(debug)Debug.Log("Damaging " + camper);
				
					if(cooldownTimer > damageCooldown){
						camper.networkView.RPC("DamageRPC", RPCMode.All, damage, Vector3.up);
						cooldownTimer = 0;
					}
					cooldownTimer += Time.deltaTime;
				
			}
			if(Vector3.Distance(camper.transform.position, gameObject.transform.position) > dropDistance){
				timer2 += Time.deltaTime;
				if(timer2 > timeUntilKillSwarm)
				{
					DestroySwarm();
				}
				
			}
			else{
				timer2 = 0;
				
			}
		}
	}
	
	public void informationFill(GameObject player){
		camper = player;
		if(debug)Debug.Log(camper + " is camping");
	}

	
	private void DestroySwarm(){
		Debug.Log ("Destroying Swarm eventually.");
		//particles.enableEmission = false;
		//while(true){
			
			//if(particles.particleCount == 0){
				Destroy (gameObject);
			//}
		//}
	}
}
